<?php
require_once(dirname(__FILE__).'/../bootstrap/Doctrine.php');

$t = new lime_test(8, new lime_output_color());

$t->comment('->addJoinTagsQuery()');
$q = Doctrine::getTable('Todo')->addJoinTagsQuery();
$t->like($q->getSqlQuery(), '/LEFT JOIN todo_tag/', '->addJoinTagsQuery() adds a jointure with tags');


$t->comment('->getPublishedList()');
$todoList = Doctrine::getTable('Todo')->getPublishedList();
$hasNonPublishedTodo = false;
foreach ($todoList as $todo)
{
  if (false === $todo->is_published)
  {
    $hasNonPublishedTodo = true;
  }
}
$t->is($hasNonPublishedTodo, false, '->getPublishedList() returns published Todo');
try
{
  $previousPosition = 0;
  foreach ($todoList as $todo)
  {
    if ($todo->position <= $previousPosition)
    {
      throw new Exception('Not sorted by position');
    }
    $previousPosition = $todo->position;
  }
  $t->pass('->getPublishedList() returns todos sorted by position');
}
catch (Exception $e)
{
  $t->fail('->getPublishedList() returns todos sorted by position');
}

$t->comment('->getObject()');
$todo404   = Doctrine::getTable('Todo')->findOneByTitle('Customise the "Oops! Page Not Found" page');
$todo = Doctrine::getTable('Todo')->getObject(array('id' => $todo404->id));
$t->is($todo, $todo404, '->getObject() returns an object by its id and adds a jointure with tags');
try
{
  $todo = Doctrine::getTable('Todo')->getObject();
  $t->fail('->getObject() requires the id in parameters');
}
catch (Exception $e)
{
  $t->pass('->getObject() requires the id in parameters');
}

$t->comment('->getObjectBySlug()');
$todo404   = Doctrine::getTable('Todo')->findOneByTitle('Customise the "Oops! Page Not Found" page');
$todo = Doctrine::getTable('Todo')->getObjectBySlug(array('slug' => $todo404->slug));
$t->is($todo, $todo404, '->getObjectBySlug() returns an object by its slug and adds a jointure with tags');
try
{
  $todo = Doctrine::getTable('Todo')->getObjectBySlug();
  $t->fail('->getObjectBySlug() requires the slug in parameters');
}
catch (Exception $e)
{
  $t->pass('->getObjectBySlug() requires the slug in parameters');
}

$t->comment('->findByIdList()');
$todoList = Doctrine::getTable('Todo')->findAll();
$idArray = array();
foreach ($todoList as $todo)
{
  $idArray[] = $todo->id;
}
$todoFound = Doctrine::getTable('Todo')->findByIdList($idArray);
$t->is(count($todoList), count($todoFound), '->findByIdList() returns a list of todo from a list of ids');
